﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Saxxon.LibDoomSharp.Lumps
{
    public class HexenLinedefsEntry : SerializableBase
    {
        /// <summary>
        /// Length of a Linedef table entry in bytes.
        /// </summary>
        public const int ENTRY_LENGTH = 14;

        /// <summary>
        /// Starting Vertex.
        /// </summary>
        public Int16 VertexStart;

        /// <summary>
        /// Ending vertex.
        /// </summary>
        public Int16 VertexEnd;

        /// <summary>
        /// Flags that determine behavior.
        /// </summary>
        public LinedefsEntryFlags Flags;

        /// <summary>
        /// Type that determines behavior.
        /// </summary>
        public byte Action;

        /// <summary>
        /// Action argument 1.
        /// </summary>
        public byte ActionArg1;

        /// <summary>
        /// Action argument 2.
        /// </summary>
        public byte ActionArg2;

        /// <summary>
        /// Action argument 3.
        /// </summary>
        public byte ActionArg3;

        /// <summary>
        /// Action argument 4.
        /// </summary>
        public byte ActionArg4;

        /// <summary>
        /// Action argument 5.
        /// </summary>
        public byte ActionArg5;

        /// <summary>
        /// Sidedef index for the right side of the linedef.
        /// </summary>
        public Int16 RightSidedef;

        /// <summary>
        /// Sidedef index for the left side of the linedef.
        /// </summary>
        public Int16 LeftSidedef;

        /// <summary>
        /// Read Linedef data.
        /// </summary>
        protected override void Read(Stream stream, int length)
        {
            DataReader reader = new DataReader(stream);
            VertexStart = reader.ReadInt16();
            VertexEnd = reader.ReadInt16();
            Flags = (LinedefsEntryFlags)reader.ReadInt16();
            Action = reader.ReadByte();
            ActionArg1 = reader.ReadByte();
            ActionArg2 = reader.ReadByte();
            ActionArg3 = reader.ReadByte();
            ActionArg4 = reader.ReadByte();
            ActionArg5 = reader.ReadByte();
            RightSidedef = reader.ReadInt16();
            LeftSidedef = reader.ReadInt16();
        }

        /// <summary>
        /// Retrieve string representation of Linedef.
        /// </summary>
        public override string ToString()
        {
            return "Linedef { Vertex=" +
                VertexStart.ToString() + ", " + VertexEnd.ToString() +
                "; Flags=" + Flags.ToString() +
                "; Action=" + Action.ToString() + ", " +
                ActionArg1.ToString() + ", " +
                ActionArg2.ToString() + ", " +
                ActionArg3.ToString() + ", " +
                ActionArg4.ToString() + ", " + 
                ActionArg5.ToString() +
                "; RightSide=" + RightSidedef.ToString() +
                "; LeftSide=" + LeftSidedef.ToString() +
                "; }";
        }

        /// <summary>
        /// Write linedef data.
        /// </summary>
        protected override void Write(Stream stream)
        {
            DataWriter writer = new DataWriter(stream);
            writer.WriteInt16(VertexStart);
            writer.WriteInt16(VertexEnd);
            writer.WriteInt16((short)Flags);
            writer.WriteInt8(Action);
            writer.WriteInt8(ActionArg1);
            writer.WriteInt8(ActionArg2);
            writer.WriteInt8(ActionArg3);
            writer.WriteInt8(ActionArg4);
            writer.WriteInt8(ActionArg5);
            writer.WriteInt16(RightSidedef);
            writer.WriteInt16(LeftSidedef);
        }
    }
}
